"""
@name : 回合制对战游戏
@author : chenlu
@projectname: 联合国数据爬取.py
"""
class Role:
    def __init__(self, name, sex, blood=100, equip=None):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.equip = equip
        self.room = None

    def combat(self, other):
        if self.room == other.room:
            if self != other:
                if self.blood > 0 and other.blood > 0:
                    drop_blood = 5 if other.equip else 10
                    other.blood -= drop_blood
                    print(f"{self.name}攻击{other.name}{other.blood}点血量"
                          f"\t{self.name} 当前血量:{self.blood}"
                          f"\t{other.name}当前血量:{other.blood}")
                else:
                    print("其中一方已经死亡，不能攻击")
                    exit()
            else:
                print("无效攻击对象")
        else:
            print("不在同一房间，不能攻击")

class Room():
    def __init__(self, name):
        self.member = []
        self.name = name
    def join(self, role):
        if role.room == None:
            if len(self.member) < 2:
                self.member.append(role)
                role.room = self.name
            else:
                print("此房间已满")
        else:
            print("角色已经加入该房间")

role_dict = {}
room_dict = {}
while True:
    print("请选择:1.创建角色\t2.创建房间\t3.加入房间\t4.开始战斗\t5.退出")
    options = input("请输入你的选择:\t")
    if options.isdigit():
        options = int(options)
        if 1 <= options <= 5:
            if options == 1:
                name, sex = input("请输入角色名和性别，空格隔开\n").split()
                role_dict[name] = Role(name, sex)
                def_blood = input("输入自定义血量\n")
                if def_blood.isdigit():
                    role_dict[name].blood = int(def_blood)
                    print(f"用户{name}当前血量{role_dict[name].blood}")
                else:
                    print("自定义方法")
                wear_equip = input("输入y购买防弹衣\n")
                if wear_equip == "y":
                    role_dict[name].equip = 1
            elif options == 2:
                name = input("请输入房间的名字\n")
                room_dict[name] = Room(name)
            elif options == 3:
                print(f"当前角色有{role_dict.keys()},当前房间有{room_dict.keys()}")
                role_name, room_name = input("请输入要加入的角色和房间名，空格隔开\n").split()
                room_dict[room_name].join(role_dict[role_name])
            elif options == 4:
                print(f"当前房间有{room_dict.keys()}")
                room_name = input("请输入房间的名字\n")
                if len(room_dict[room_name].member) == 2:
                    while True:
                        room_dict[room_name].member[0].combat(room_dict[room_name].member[1])
                        room_dict[room_name].member[1].combat(room_dict[room_name].member[0])
                else:
                    print(f"房间{room_name}人数不足2人，对战失败!")
                    continue
            elif options == 5:
                print("退出战斗")
                break
        else:
            print("输入不合法，请输入1-5重新操作")
    else:
        print("输入不合法，请输入1-5选择操作")
